/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Preview.java
 *
 * Created on 2010-feb-03, 20:36:21
 */
package exmtgproj;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JTextArea;

/**
 * målar upp det previewade kortet på Preview panelen
 * och skriver ut texten från det på PreviewText panelen
 * @author marcus ahlen
 */
public class Preview extends javax.swing.JPanel implements Statics {

    /** Creates new form Preview */
    public Preview() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    static int cardIndex = 0;
    static Card prevPrevCard = null;
    static Card prevCard = null;
    BufferedImage prevImg = null;
    //static Image prevImg = Images.back;
    static String prevImgName = null;

    @Override
    public void paint(Graphics g) {
        if (prevImg == null) {
            prevImg = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        }
        updatePreview();
        prevPrevCard = prevCard;
        g.drawImage(prevImg, 0, 0, null);

    }

    /**
     * hämtar text från ett kort
     * @param card ett kort
     * @return text
     */
    public static String getNewText(Card card) {
        String s = "|||";

        if (card != null) {
            //name
            s += card.name + "\n";
            //cost
            String mana = "";
            if (card.cast.xCost) {
                mana += "{X}";
            }
            if (card.cast.cost[c] != 0) {
                mana += "{" + card.cast.cost[c] + "}";
            }
            for (int i = 0; i < card.cast.cost[w]; i++) {
                mana += "{W}";
            }
            for (int i = 0; i < card.cast.cost[u]; i++) {
                mana += "{U}";
            }
            for (int i = 0; i < card.cast.cost[b]; i++) {
                mana += "{B}";
            }
            for (int i = 0; i < card.cast.cost[r]; i++) {
                mana += "{R}";
            }
            for (int i = 0; i < card.cast.cost[g]; i++) {
                mana += "{G}";
            }
            if (mana.equals("") && !card.type[land]) {
                mana = "{0}";
            }
            s += mana;
            s += "\n";
            //type
            if (card.legendary) {
                s += "Legendary ";
            }
            if (card.basic) {
                s += "Basic ";
            }
            if (card.type[land]) {
                s += "Land ";
            }
            if (card.type[creature]) {
                s += "Creature ";
            }
            if (card.type[enchantment]) {
                s += "Enchantment ";
            }
            if (card.type[artifact]) {
                s += "Artifact ";
            }
            if (card.type[planeswalker]) {
                s += "Planeswalker ";
            }
            if (card.type[sorcery]) {
                s += "Sorcery ";
            }
            if (card.type[instant]) {
                s += "Instant ";
            }
            if (card.subtype.size() != 0) {
                s += "- ";
                for (int i = 0; i < card.subtype.size(); i++) {
                    s += card.subtype.get(i) + " ";
                }
            }
            s += "\n";
            //power/toughness
            if (card.type[creature]) {
                s += card.power + "/" + card.toughness;
            }
            s += "\n";
            //edition (rarity)
            s += card.edition + " (" + card.rarity + ")";
            s += "\n";
            s += "\n";
            if (card.basic) {
                s += "{" + card.statics.get(0) + "}";
            } else {
                //abilities
                s += "///";
                for (Ability a : card.abilities) {
                    // s += i++ + ": \n";
                    s += a.spellText;
                    s += "\n\n";
                }
                s += "---";
                try {
                    for (ArrayList<Ability> list : card.events) {
                        for (Ability a : list) {
                            s += a.spellText;
                            s += "\n\n";
                        }
                    }
                } catch (Exception e) {
                }
                s += "|||";
                for (Ability a : card.statics) {
                    s += a.spellText;
                    s += "\n\n";
                }
            }
        }
        return s;
    }
    Color[] colors = {Color.black, Color.blue.darker().darker(), Color.red.darker().darker()};
    int color = 0;

    private void updatePreview() {
        Graphics g = prevImg.getGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.black);
        g.fillRect(0, 0, getWidth(), getWidth() / 12 * 17);  //målar ut en svart box i väntan på att den riktiga bilden ska laddas
        String s = "";
        if (prevCard != null && !prevCard.facedown) {
            s = getNewText(prevCard);
            prevCard.draw(g, 0, 0, getWidth(), getWidth() / 12 * 17, false);
        } else {
            s = "";
            g.drawImage(Images.back, 0, 0, getWidth(), getWidth() / 12 * 17, this);
        }


        if (prevCard == null || prevCard.facedown) {
            g.drawImage(Images.back, 0, 0, getWidth(), getWidth() / 12 * 17, null);
        } else {
            g.setFont(Font.decode("arial-15"));
            boolean italic = false;
            int x = 5;
            int h = g.getFontMetrics().getHeight();
            int y = getWidth() / 12 * 17 + h + 5;
            int w = getWidth() - x;
            String iconWidth = "";
            while (g.getFontMetrics().stringWidth(iconWidth) < 15) {
                iconWidth += " ";
            }
            ArrayList<String> str = new ArrayList<String>();
            ArrayList<Point> iconPos = new ArrayList<Point>();
            ArrayList<String> icon = new ArrayList<String>();
            Scanner scan = new Scanner(s);

            while (scan.hasNextLine()) {
                String res = "";
                Scanner sc = new Scanner(scan.nextLine());

                while (sc.hasNext()) {
                    String next = sc.next();
                    if (g.getFontMetrics().stringWidth(res + next + " ") > w) {
                        str.add(res);
                        res = "";
                    }

                    while (next.length() > 0 && next.contains("{") && next.contains("}")) {
                        int i = 0;
                        for (; i < next.length(); i++) {
                            if (next.charAt(i) == '{') {
                                res += next.substring(0, i);
                                next = next.substring(i);
                                break;
                            }
                        }
                        for (; i < next.length(); i++) {
                            if (next.charAt(i) == '}') {
                                break;
                            }
                        }
                        if (i < next.length()) {
                            icon.add(next.substring(1, i).replace("/", ""));
                            iconPos.add(new Point(x + g.getFontMetrics().stringWidth(res), str.size()));
                            res += iconWidth;
                            if (next.length() <= i + 1) {
                                next = "";
                            } else {
                                next = next.substring(i + 1);
                            }
                        }
                    }
                    res += next + " ";
                }
                str.add(res);
            }
            int j = 0;
            for (int i = 0; i < str.size(); i++) {
                int offset = 0;
                if (str.get(i).contains("///")) {
                    str.set(i, str.get(i).replace("///", ""));
                    color = 1;
                    offset += g.getFontMetrics().stringWidth("///");
                }
                if (str.get(i).contains("---")) {
                    str.set(i, str.get(i).replace("---", ""));
                    color = 2;
                    offset += g.getFontMetrics().stringWidth("---");
                }
                if (str.get(i).contains("|||")) {
                    str.set(i, str.get(i).replace("|||", ""));
                    color = 0;
                    offset += g.getFontMetrics().stringWidth("|||");
                }
                g.setColor(colors[color]);
                g.drawString(str.get(i), x, y + i * h);
                while (j < iconPos.size() && iconPos.get(j).y <= i) {
                    if (iconPos.get(j).y == i) {
                        g.drawImage(Images.getIconImage(icon.get(j)), iconPos.get(j).x + (g.getFontMetrics().stringWidth(iconWidth) - 15) / 2-offset, y + (i-1) * h + 4, null);
                    }
                    j++;
                }
            }
        }

        /*Scanner sc = new Scanner(s);
        for()*/

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
